package com.bishe.psy.exception;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**全局异常处理的类
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-03-21
 * Time: 19:12
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(ApplicationException.class)
    public AppResult applicationExceptionHandler(ApplicationException e) {
        e.printStackTrace();
        log.error(e.getMessage());
        if(e.errorResult != null) {
            return e.getErrorResult();
        }

        if(e.getMessage() == null || e.getMessage().equals("")) {
            return AppResult.failed(ResultCode.ERROR_SERVICES);
        }
        return AppResult.failed(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public AppResult ExceptionHandler(Exception e) {
        e.printStackTrace();
        log.error(e.getMessage());
        if(e.getMessage() == null || e.getMessage().equals("")) {
            return AppResult.failed(ResultCode.ERROR_SERVICES);
        }
        return AppResult.failed(e.getMessage());
    }
}
